package org.yoki.edu.bg.radius.filter.app.transit;

import org.yoki.edu.bg.radius.filter.app.obs.SynObservable;
import org.yoki.edu.bg.radius.filter.app.obs.SynObserver;
import org.yoki.edu.bg.radius.filter.app.operator.SynPacketOperator;
import org.yoki.edu.bg.radius.filter.app.receiver.packet.RadiusPacket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2018/1/4$ 13:58$
 */
public abstract class SynPacketTransit extends SynObservable<SynPacketOperator> implements SynObserver {

    /**
     * 数据包转发给Observers前的数据处理
     *
     * @param packet
     * @return
     */
    protected abstract RadiusPacket processPacket(RadiusPacket packet);

    @Override
    public void update(SynObservable o, Object... args) {
        List<RadiusPacket> packets = new ArrayList<>();
        if (null != args && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                Object arg = args[i];
                RadiusPacket packet = processPacket((RadiusPacket) arg);
                if (null != packet) {
                    packets.add(packet);
                }
            }
        }
        Object[] arr = packets.toArray();
        setChanged();
        notifyObservers(arr);
    }

}


